package myStack;

import java.util.Arrays;

public class MyStack {
    private int DEFAULT_SIZE = 5;
    private int[] elem;
    private int usedSize;
    public MyStack() {
        elem = new int[DEFAULT_SIZE];
    }
    public void push(int val) {
        if(elem.length == usedSize) {
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    public int peek() {
        if(isEmpty()) {
            throw new StackNullException("栈为空...");
        }
        return  elem[usedSize-1];
    }
    public int pop() {
        if(usedSize <= 0) {
            throw new StackNullException("栈为空，无法进行出栈操作...");
        }
        int val = elem[usedSize-1];
        usedSize--;
        return val;
    }
    public boolean isEmpty() {
        return usedSize==0;
    }
}
